var express = require('express');
var http = require("http");
var path = require('path');
var session = require("express-session");
var bodyParser = require('body-parser');

const NinelineService = require("./services/NinelineService")

var redis = require("./utils/redis")
var redisRpc = require("./utils/redisTaozi")


var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html',require("ejs").renderFile);
app.set('view engine', 'html');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());

app.use(session({
    secret : "personnel",
}));

redis.init({
    host : "123.57.129.108",
    port : "6379",
    password:"ARspjl9tQFIfI",
})

// redisRpc.init({
//     HOST : "172.31.132.132",
//     port : "6118",
//     password : "1OHu%F$XY5%ecC!V",
//     db:1
// })

app.use(express.static(path.join(__dirname, 'public')));



app.get("/client", function(req, res){
    res.render("client")
})

app.get("/baxitest", function(req, res){
    res.render("nineline")
})

app.get("/truco", function(req, res){
    res.render("truco")
})

app.get("/trucoTest", function(req, res){
    res.render("trucoTest")
})

app.post("/game/start", async function(req, res){
    try{
        let {userId, betAmount} = req.body;
        userId = +userId || 1;
        let result = await new NinelineService().startGame(userId, betAmount)
        res.send(result)
    }catch(err){
        console.log(err)
    }
})

app.post("/game/stat", async function(req, res){
    try{
        let {userId} = req.body;
        let result = await new NinelineService().getStat(userId)
        res.send(result)
    }catch(err){
        console.log(err)
    }
})

app.post("/game/clearUserData", async function(req, res){
    try{
        let {userId} = req.body;
        let result = await new NinelineService().clearUserData(userId)
        res.send(result)
    }catch(err){
        console.log(err)
    }
})

app.post("/game/clearSystemData", async function(req, res){
    try{
        let {userId} = req.body;
        let result = await new NinelineService().clearSystemData(userId)
        res.send(result)
    }catch(err){
        console.log(err)
    }
})

var server = http.createServer(app).listen(3737,function(){
    console.log('Express server listening on port ' + 3737);
});
module.exports = app